package com.cm309.hospital.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cm309.hospital.dao.BookingDao;
import com.cm309.hospital.dao.DoctorDao;
import com.cm309.hospital.dao.DoctorScheduleDao;
import com.cm309.hospital.dao.PatientDao;
import com.cm309.hospital.dao.SubjectDao;
import com.cm309.hospital.model.Booking;
import com.cm309.hospital.model.DoctorSchedule;
import com.cm309.hospital.model.Patient;

@WebServlet("/patientBook.do")
public class PatientBook extends HttpServlet{

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String subject_id = req.getParameter("subject_id");
		String patient_id = req.getParameter("patient_id");
		String doctor_id = req.getParameter("doctor_id");
		String doctor_schedule_id = req.getParameter("doctor_schedule_id");
		
		Patient patient = new Patient();
		PatientDao patientDao = new PatientDao();
		patient = patientDao.querySingle(patient_id);
		System.out.println("3");		

		
			req.setAttribute("patient",patient);
			
//			List subjects = new ArrayList();
			SubjectDao subjectDao = new SubjectDao();
//			subjects = subjectDao.queryAll();
//			req.setAttribute("subjects",subjects);
					
			req.setAttribute("subject_id", subject_id);
			req.setAttribute("subject",subjectDao.querySingle(subject_id));			

//			List doctors = new ArrayList();
			DoctorDao doctorDao = new DoctorDao();
//			doctors = doctorDao.queryDoctorsBySubject(subject_id);
//System.out.println("size of doctors :"+doctors.size());			
//			req.setAttribute("doctors",doctors);

			req.setAttribute("doctor", doctorDao.querySingle(doctor_id));

			DoctorScheduleDao scheduleDao = new DoctorScheduleDao();					
			DoctorSchedule doctorSchedule = scheduleDao.queryByScheduleId(doctor_schedule_id);
			req.setAttribute("doctorSchedule", doctorSchedule);
			
			Booking booking = new Booking();
			booking.setPatient_id(Integer.valueOf(patient_id).intValue());
			booking.setDoctor_schedule_id(Integer.valueOf(doctor_schedule_id).intValue());
			
			BookingDao bookDao = new BookingDao();
			bookDao.add(booking);
			
			req.getRequestDispatcher("view/patientBook.jsp").forward(req, resp);
//		}
//		else
//		{
//			req.getRequestDispatcher("mainPage.do").forward(req,resp);
//		}
	}


}
